package com.wzk.travelspringboot.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wzk.travelspringboot.dto.Result;
import com.wzk.travelspringboot.entity.ScenicType;
import com.wzk.travelspringboot.service.ScenicTypeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * @author wangzikang
 */
@Slf4j
@RestController
@RequestMapping("/admin/scenicType")
@RequiredArgsConstructor
@Validated
public class AdminScenicTypeController {

    private final ScenicTypeService scenicTypeService;

    /**
     * 查询所有景区类型
     */
    @GetMapping("/list")
    public Result listAllTypes() {
        return scenicTypeService.getAllType();
    }

    /**
     * 添加景区类型
     */
    @PostMapping("/add")
    public Result addScenicType(@RequestParam @NotBlank(message = "类型名称不能为空") String typeName) {
        return scenicTypeService.insertType(typeName);
    }

    /**
     * 删除景区类型
     */
    @DeleteMapping("/remove/{id}")
    public Result deleteScenicType(@PathVariable("id") @NotNull(message = "类型ID不能为空") Integer id) {
        boolean success = scenicTypeService.removeById(id);
        return success ? Result.success("删除景区类型成功") : Result.failed("删除景区类型失败");
    }

    /**
     * 更新景区类型
     */
    @PostMapping("/update")
    public Result updateScenicType(
            @RequestParam @NotNull(message = "类型ID不能为空") Integer id,
            @RequestParam @NotBlank(message = "类型名称不能为空") String typeName) {
        ScenicType scenicType = new ScenicType();
        scenicType.setId(id);
        scenicType.setTypeName(typeName);
        boolean success = scenicTypeService.updateById(scenicType);
        return success ? Result.success("更新景区类型成功") : Result.failed("更新景区类型失败");
    }

    /**
     * 根据ID查询景区类型
     */
    @GetMapping("/detail/{id}")
    public Result getScenicTypeById(@PathVariable("id") @NotNull(message = "类型ID不能为空") Integer id) {
        ScenicType scenicType = scenicTypeService.getById(id);
        if (scenicType == null) {
            return Result.failed("景区类型不存在");
        }
        return Result.success(scenicType);
    }

    /**
     * 根据名称查询景区类型
     */
    @GetMapping("/search")
    public Result searchScenicTypeByName(@RequestParam @NotBlank(message = "类型名称不能为空") String typeName) {
        LambdaQueryWrapper<ScenicType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(ScenicType::getTypeName, typeName);
        return Result.success(scenicTypeService.list(queryWrapper));
    }
} 